package model

import (
	"github.com/gomodule/redigo/redis"
	"strconv"
	"time"
	"tizhi_front/nspel"
)

type Prescription struct {
	Id       int64     `gorm:"primary_key;column:id"`
	Name     string    `gorm:"column:name"`
	LabelId  int64     `gorm:"column:label_id"`
	Label    string    `gorm:"column:label"`
	Actions  string    `gorm:"column:actions"`
	PicUrl   string    `gorm:"column:pic_url"`
	Level0   bool      `gorm:"column:level_0"`
	Level1   bool      `gorm:"column:level_1"`
	Level2   bool      `gorm:"column:level_2"`
	Level3   bool      `gorm:"column:level_3"`
	Level4   bool      `gorm:"column:level_4"`
	Level5   bool      `gorm:"column:level_5"`
	Level6   bool      `gorm:"column:level_6"`
	Male     bool      `gorm:"column:male"`
	Female   bool      `gorm:"column:female"`
	Grade11  bool      `gorm:"column:grade_11"`
	Grade12  bool      `gorm:"column:grade_12"`
	Grade13  bool      `gorm:"column:grade_13"`
	Grade14  bool      `gorm:"column:grade_14"`
	Grade15  bool      `gorm:"column:grade_15"`
	Grade16  bool      `gorm:"column:grade_16"`
	Grade21  bool      `gorm:"column:grade_21"`
	Grade22  bool      `gorm:"column:grade_22"`
	Grade23  bool      `gorm:"column:grade_23"`
	Grade31  bool      `gorm:"column:grade_31"`
	Grade32  bool      `gorm:"column:grade_32"`
	Grade33  bool      `gorm:"column:grade_33"`
	CreateAt time.Time `gorm:"column:create_at"`
	UpdateAt time.Time `gorm:"column:update_at"`
}

func (p *Prescription) TableName() string {
	return "prescription"
}

type Prescription0 struct {
	Id      int64  `gorm:"primary_key;column:id"`
	Name    string `gorm:"column:name"`
	LabelId int64  `gorm:"column:label_id"`
	Actions string `gorm:"column:actions"`
	PicUrl  string `gorm:"column:pic_url"`
}

func (p *Prescription0) TableName() string {
	return "prescription"
}

func (p *Prescription0) SelectByLabelIdLevelSexGrade(labelId int64, level string, sex string, grade string) (err error) {
	p.Id = 0
	key := "prescription0.label_id:" + strconv.FormatInt(labelId, 10) + "." + level + "." + sex + "." + grade
	err = nspel.RedisGetGob(key, p)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select([]string{"id", "name", "label_id", "actions", "pic_url"}).Where("label_id = ?", labelId).Where(level+" = ?", true).Where(sex+" = ?", true).Where(grade+" = ?", true).First(p).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, p)
	return
}

type Prescription1 struct {
	Name    string `gorm:"column:name"`
	LabelId int64  `gorm:"column:label_id"`
	Actions string `gorm:"column:actions"`
	PicUrl  string `gorm:"column:pic_url"`
}

func (p *Prescription1) TableName() string {
	return "prescription"
}

func (p *Prescription1) SelectById(id int64) (err error) {
	key := "prescription1.id:" + strconv.FormatInt(id, 10)
	err = nspel.RedisGetGob(key, p)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select([]string{"name", "label_id", "actions", "pic_url"}).Where("id = ?", id).First(p).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, p)
	return
}
